#include "ui_NestConfigDialog.h"
#include "QDebug"
#include "QString"
#include "NestConfigDialog.h"

NestConfigDialog::NestConfigDialog( NestConfig *nestConfig, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NestConfigDialog),
    nestConfig(nestConfig)
{
    ui->setupUi(this);
    ui->angle_scale->addItem("0");
    for(int i =1; i<=10;i++){
        if(180%i==0){
            ui->angle_scale->addItem(QString::number(i));
        }
    }

    ui->radio_button_GA->setChecked(true);  // 默认选择GA
    ui->max_loop->setValue(nestConfig->MAX_LOOP);
    ui->max_loop->setMaximum(1000);
    ui->population_size->setValue(nestConfig->POPULATION_SIZE);
    int angle_scale_index = ui->angle_scale->findText(QString::number(nestConfig->ANGLE_SCALE));
    if(angle_scale_index==-1){
        qDebug() << "no data in angle scale";
    }
    ui->angle_scale->setCurrentIndex(angle_scale_index);
    ui->mutation_rate->setValue(static_cast<int>(nestConfig->MUTATION_RATE*100));
    ui->SA_alpha->setRange(0.6, 0.99);
    ui->SA_alpha->setValue(nestConfig->ALPHA);
    ui->SA_inner_iteration->setMaximum(1000);
    ui->SA_outer_iteration->setMaximum(1000);
    ui->SA_inner_iteration->setValue(nestConfig->MAX_INNER_ITERATION);
    ui->SA_outer_iteration->setValue(nestConfig->MAX_OUTER_ITERATION);
    ui->SA_t_0->setMaximum(1000);
    ui->SA_t_0->setValue(nestConfig->T_0);
    ui->SA_t_n->setMaximum(500);
    ui->SA_t_n->setValue(nestConfig->END_TEMP);


    ui->curve_tolerance->setMaximum(10);
    ui->curve_tolerance->setValue(nestConfig->TOLERANCE);
    ui->inflate_delta->setValue(nestConfig->INFLATE_DELTA);
    ui->inflate_delta->setRange(-10, 10);

    this->setWhatsThis("这是提示");

}

NestConfigDialog::~NestConfigDialog()
{
    qDebug()<<"析构函数";
    delete ui;
}

void NestConfigDialog::on_button_confirm_clicked()
{

    nestConfig->ANGLE_SCALE = ui->angle_scale->currentText().toInt();
    nestConfig->MAX_LOOP = ui->max_loop->value();
    nestConfig->MUTATION_RATE = static_cast<double>(ui->mutation_rate->value())/100;
    nestConfig->POPULATION_SIZE = ui->population_size->value();
    nestConfig->ALPHA = ui->SA_alpha->value();
    nestConfig->MAX_INNER_ITERATION = ui->SA_inner_iteration->value();
    nestConfig->MAX_OUTER_ITERATION = ui->SA_outer_iteration->value();
    nestConfig->T_0 = ui->SA_t_0->value();
    nestConfig->END_TEMP = ui->SA_t_n->value();
    nestConfig->TOLERANCE = ui->curve_tolerance->value();
    nestConfig->INFLATE_DELTA = ui->inflate_delta->value();

    if(ui->radio_button_GA->isChecked()){
        emit start_nest(1);
    }else if(ui->radio_button_SA->isChecked()){
        emit start_nest(2);
    }

    if (nestConfig == nullptr) {
        qDebug()<<"nestconfigdialog出错了";
        return;
    }

    this->close();

}

void NestConfigDialog::on_button_cancel_clicked()
{
    this->close();
}

